﻿using MotionControl;
using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Threading;
using System.Threading.Tasks;
using System.Windows.Forms;

namespace MainFrame
{
    public partial class FormIOCheck : Form
    {
        public FormIOCheck()
        {
            InitializeComponent();
        }
        private List<CheckBox> input_card = new List<CheckBox>();
        private List<CheckBox> input_card1 = new List<CheckBox>();
        private List<CheckBox> input_card2 = new List<CheckBox>();
        private List<CheckBox> output_card = new List<CheckBox>();
        private List<CheckBox> output_card1 = new List<CheckBox>();
        private List<CheckBox> output_card2 = new List<CheckBox>();
        private void FormIOCheck_Load(object sender, EventArgs e)
        {
            input_card = GetCheckBoxList(groupBox2);
            input_card1 = GetCheckBoxList(groupBox3);
            input_card2 = GetCheckBoxList(groupBox4);
            output_card = GetCheckBoxList(groupBox8);
            output_card1 = GetCheckBoxList(groupBox7);
            output_card2 = GetCheckBoxList(groupBox6);
            SetCheckBoxList(input_card, Enum.GetNames(typeof(e_input_card1)));
            SetCheckBoxList(input_card1, Enum.GetNames(typeof(e_input_extCard1)));
            SetCheckBoxList(input_card2, Enum.GetNames(typeof(e_input_extCard2)));
            SetCheckBoxList(output_card, Enum.GetNames(typeof(e_output_card1)));
            SetOutputList(output_card, 0);
            SetCheckBoxList(output_card1, Enum.GetNames(typeof(e_output_extCard1)));
            SetOutputList(output_card1, 1);
            SetCheckBoxList(output_card2, Enum.GetNames(typeof(e_output_extCard2)));
            SetOutputList(output_card2, 2);

            Task.Run(Task_RefreshIO);
        }
     
        private void Task_RefreshIO()
        {
            try
            {
               while(!GlobalVar.isExit)
                {
                    Thread.Sleep(300);
                    Application.DoEvents();
                    this.BeginInvoke(
                        new Action(()=> { 
                           for(int i=0;i<input_card.Count;i++)
                            {
                                input_card[i].Checked = GlobalMotion.Input_Card1[i];
                                input_card[i].BackColor = GlobalMotion.Input_Card1[i]?Color.Green:Color.Gray;
                            }
                            for (int i = 0; i < input_card1.Count; i++)
                            {
                                input_card1[i].Checked = GlobalMotion.Input_CardEx1[i];
                                input_card1[i].BackColor = GlobalMotion.Input_CardEx1[i] ? Color.Green : Color.Gray;
                            }
                            for (int i = 0; i < input_card2.Count; i++)
                            {
                                input_card2[i].Checked =GlobalMotion.Input_CardEx2[i];
                                input_card2[i].BackColor = GlobalMotion.Input_CardEx2[i] ? Color.Green : Color.Gray;
                            }
                            for (int i = 0; i < output_card.Count; i++)
                            {
                                output_card[i].Checked = GlobalMotion.Output_Card1[i];
                                output_card[i].BackColor = GlobalMotion.Output_Card1[i] ? Color.Green : Color.Gray;
                            }
                            for (int i = 0; i < output_card1.Count; i++)
                            {
                                output_card1[i].Checked = GlobalMotion.Output_CardEx1[i];
                                output_card1[i].BackColor = GlobalMotion.Output_CardEx1[i] ? Color.Green : Color.Gray;
                            }
                            for (int i = 0; i < output_card2.Count; i++)
                            {
                                output_card2[i].Checked = GlobalMotion.Output_CardEx2[i];
                                output_card2[i].BackColor = GlobalMotion.Output_CardEx2[i] ? Color.Green : Color.Gray;
                            }
                            Refresh();
                        })
                        );
                }
            }catch(Exception ex)
            {
                displalyInfo.displayErrorMain("Task_RefreshIO"+ex.Message);
            }
        }
       
        private List<CheckBox>GetCheckBoxList(GroupBox group)
        {
            List<CheckBox> ckb_list = new List<CheckBox>();
            foreach (var curBox in group.Controls)
            {
                if (curBox is CheckBox)
                    ckb_list.Add((CheckBox)curBox);
            }
            return ckb_list.OrderBy(a => a.TabIndex).ToList();
        }
        private void SetCheckBoxList(List<CheckBox>list,string[] arrNames)
        {
          
            for(int i=0;i< list.Count;i++)
            {
                if (i < arrNames.Length)
                    list[i].Text = arrNames[i];
                else
                    list[i].Text = "预留";
            }
        }
        private void SetOutputList(List<CheckBox> list,int cardno)
        {
            for (int i = 0; i < list.Count; i++)
            {
                if(cardno==0)
                    list[i].Click += checkBox0_Click;
                else if(cardno == 1)
                    list[i].Click += checkBox1_Click;
                else if (cardno == 2)
                    list[i].Click += checkBox2_Click;
            }
        }

        private void checkBox0_Click(object sender, EventArgs e)
        {
            var curCheck = (CheckBox)sender;
            var curIndex = curCheck.TabIndex;
            GlobalMotion.Card_1.setOut((ushort)curIndex,!curCheck.Checked?e_out_OnOff.on:e_out_OnOff.off);
        }
        private void checkBox1_Click(object sender, EventArgs e)
        {
            var curCheck = (CheckBox)sender;
            var curIndex = curCheck.TabIndex;
            GlobalMotion.CardExt_1.setOut((ushort)curIndex, !curCheck.Checked ? e_out_OnOff.on : e_out_OnOff.off);
        }
        private void checkBox2_Click(object sender, EventArgs e)
        {
            var curCheck = (CheckBox)sender;
            var curIndex = curCheck.TabIndex;
            GlobalMotion.CardExt_2.setOut((ushort)curIndex, !curCheck.Checked ? e_out_OnOff.on : e_out_OnOff.off);
        }
    }
}
